<!--
 * @Author: yaoling 1871883713@qq.com
 * @Date: 2022-10-20 18:11:21
 * @LastEditors: yaoling 1871883713@qq.com
 * @LastEditTime: 2022-12-19 13:50:51
 * @FilePath: \company-world\src\views\menuManagement\components\MenuHeader.vue
 * @Description: 菜单表格
-->
<template>
  <div class="menu-table">
    <!-- 新增头部 -->
    <div class="title-button">
      <div class="title">菜单管理</div>
      <div class="button">
        <el-button v-if="usePermission('menu.add')" :icon="Plus" color="#6C52EA" @click.stop="handleAddClick">新增</el-button>
      </div>
    </div>
    <!-- 表格 -->
    <div class="table-content">
      <el-table
        class="table"
        v-loading="loading"
        default-expand-all
        ref="multipleTableRef"
        :data="dataList"
        style="width: 100%"
        row-key="id"
        :header-cell-style="{
          background: 'rgb(250, 250, 250)',
          fontSize: '14px',
          fontWeight: '700',
          letterSpacing: '0px',
          lineHeight: '0px',
          color: 'rgba(55, 59, 69, 1)',
          textAlign: 'center'
        }"
        :cell-style="{
          fontSize: '14px',
          fontWeight: '400',
          letterSpacing: '0px',
          lineHeight: '0px',
          color: 'rgba(49, 53, 64, 1)',
          textAlign: 'center'
        }"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <p>{{ dataList }}</p>
        <el-table-column type="selection" width="55" />
        <el-table-column label="菜单名称">
          <template #default="{ row }">
            <span>
              <i :class="['iconfont', row.icon]" class="icon-color" />
              {{ row.name }}
            </span>
          </template>
        </el-table-column>
        <el-table-column property="menu_code" label="菜单编码" />
        <el-table-column property="id" label="排序" width="70" />
        <el-table-column prop="type" label="菜单类型" width="100" filter-placement="bottom-end">
          <template #default="scope">
            <el-tag :color="getColor(scope.row.type)" disable-transitions class="tag">{{
              scope.row.type
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column property="is_show" label="可见" width="70">
          <template #default="{ row }">
            <span>{{ row.is_show ? '显示' : '隐藏' }}</span>
          </template>
        </el-table-column>
        <el-table-column property="permission_code" label="权限标识"></el-table-column>
        <el-table-column property="path" label="组件路由" />
        <el-table-column property="menu_remarks" label="备注" />
        <el-table-column label="操作" width="150px">
          <template #default="{ row }">
            <el-popconfirm title="确定要删除这个菜单吗？" @confirm="removeRow(row.id)">
              <template #reference><el-button :disabled="!usePermission('menu.delete')" link type="danger">删除</el-button></template>
            </el-popconfirm>
            <el-button link :disabled="!usePermission('menu.updata')" @click="onEdit(row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref, defineExpose, reactive, PropType, watch } from 'vue'
import { Itype } from '@/type/index'
import { Plus } from '@element-plus/icons-vue'
import { usePermission } from '@/hooks/usePermission'
import { useRouter } from 'vue-router'
import { getMenuList, deleteMenuData } from '@/apis/jurisdiction/menu'
import { ElMessage } from 'element-plus'
/**
 * @desc:新增菜单
 */
const router = useRouter()
const handleAddClick = () => {
  router.push({
    path: '/menu/add-menu'
  })
}
/**
 * @desc:获取菜单
 */
interface Isearch {
  [key: string]: any
}
let search: Isearch = ref()
const loading = ref(false)
let dataList: Itype = ref()
let searchList: Isearch = ref([])
const getData = (data?: string) => {
  loading.value = true
  getMenuList()
    .then((res) => {
      if (res) {
        dataList.value = res
        dataList.value.forEach((element: Itype) => {
          element.created_at = new Date(
            +new Date(new Date(element.created_at).toJSON()) + 8 * 3600 * 1000
          )
            .toISOString()
            .replace(/T/g, ' ')
            .replace(/\.[\d]{3}Z/, '')
        })
        if (data) {
          dataList.value = dataList.value.filter((item: Itype) => {
            return filterItem(item, data)
          })
        }
        loading.value = false
      }
      loading.value = false
    })
    .catch(() => {
      ElMessage('服务器内部错误')
    })
}
getData()
/**
 * @desc: 筛选数据
 */
const filterItem = (item: Itype, name: string) => {
  if (item.name.indexOf(name) !== -1) return true
  if (item.children) {
    item.children = item.children.filter((sonEle: Itype) => {
      return filterItem(sonEle, name)
    })
    if (item.children.length > 0) {
      return true
    }
  }
  return false
}
/**
 * @desc: 菜单类型颜色
 */
const getColor = (type: string) => {
  if (type === '菜单') {
    return '#E4D155'
  } else if (type === '菜单组') {
    return '#C18DFF'
  } else {
    return '#FF779A'
  }
}
/**
 * @desc:删除
 *
 */
const removeRow = (id: number) => {
  let dataId = id
  deleteMenuData(dataId)
    .then((res) => {
      if (res) {
        getData()
      }
    })
    .catch(() => {
      ElMessage('服务器内部错误')
    })
}
/**
 * @desc:打开编辑弹窗
 */
let activeList: Itype = ref()
const onEdit = (row: []) => {
  activeList.value = row
  const data: string = JSON.stringify(activeList.value)
  router.push({
    path: '/menu/add-menu',
    query: {
      id: activeList.value.id,
      conObj: encodeURIComponent(data)
    }
  })
}
/**
 * @desc:筛选
 */
const onSearch = (data: string) => {
  searchList.value = []
  getData(data)
}
/**
 * @desc: 筛选数据
 */
const filterData = (dataObj: Isearch, searchObj: Isearch, data: string) => {
  dataObj.forEach((item: Isearch) => {
    if (item.name.includes(data)) {
      searchObj.push(item)
    }
    if (item.children && item.children.length > 0) {
      filterData(item.children, searchList.value, data)
    }
  })
}
/**
 * @desc:重置
 */
const onRefresh = () => {
  search.value = []
  getData()
}
defineExpose({
  getData,
  onRefresh
})
</script>

<style lang="less" scoped>
:deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  border-color: #6c52ea;
  background-color: #6c52ea;
}
:deep(.el-checkbox__input.is-indeterminate .el-checkbox__inner) {
  border-color: #6c52ea;
  background-color: #6c52ea;
}
:deep(.el-checkbox__inner:hover) {
  border-color: #6c52ea;
}
:deep(.el-table) {
  thead {
    height: 48px;
  }
  .cell {
    overflow: hidden;
    padding: 0 3px;
    box-sizing: border-box;
    text-overflow: ellipsis;
    word-break: break-all;
    line-height: 23px;
    white-space: nowrap;
    white-space: normal;
  }
  .icon-color {
    color: #6c52ea;
  }
}
.menu-table {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  width: 100%;
  height: 100%;
  margin-top: 8px;
  padding: 8px 18px 8px 10px;
  box-sizing: border-box;
  background-color: #fff;
  .table-content {
    overflow: auto;
    flex-grow: 1;
    width: 100%;
    height: 0;
    flex-shrink: 0;
    .table {
      .el-tag {
        display: inline-block;
        height: 22px;
        padding: 0 8px;
        box-sizing: border-box;
        background-color: #ecf5ff;
        color: #fff;
        line-height: 22px;
        font-size: 12px;
        border-width: 1px;
        border-style: solid;
        border-radius: 4px;
        border-color: #d9ecff;
        white-space: nowrap;
      }
    }
  }
}
.el-table__header-wrapper {
  border: 1px solid #ff0000;
}
.title-button {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  box-sizing: border-box;
  .title {
    color: rgba(0, 0, 0, 0.65);
    font-size: 18px;
    font-weight: 700;
    transition: all 0.3s;
  }
  .button {
    .el-button {
      height: 28px;
      font-size: 13px;
    }
  }
}
</style>
